package by.epam.communalservices.special;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Resource manager for GET static resources.
 * @author Andrei_Leonau
 * @version %I%, %G% %U%
 */
public class ResourceManager {
    /** Current selected file name. */
    private String selector = "";
    /** Current selected resource. */
    private ResourceType select = null;
    /** Absolute path prefix. */
    private final String pathURI;
    /** Resource logic collection. */
    private final Map<String, ResourceType> typeMap =
            new HashMap<String, ResourceType>();

    /**
     * Default constructor.
     * @param path
     *            absolute path setter. Required.
     */
    public ResourceManager(final String path) {
        this.pathURI = path;
    }

    /**
     * Sub class for mime store. That class may store more parameters, but
     * currently used for one.
     * @author Andrei_Leonau
     * @see ResourceManager
     */
    public class ResourceType {
        /** Resource mimeType. */
        private String mime;

        /**
         * Default constructor.
         * @param mimeType
         *            set string MimeType and/or CharSet option.
         */
        public ResourceType(final String mimeType) {
            // setMime(mimeType);
            this.mime = mimeType;
        }

        /**
         * Setter method.
         * @param mimeType
         *            set string MimeType and/or CharSet option.
         */
        public final void setMime(final String mimeType) {
            this.mime = mimeType;
        }

        /**
         * Getter method.
         * @return resource mime.
         */
        public final String getMime() {
            return this.mime;
        }
    }

    /**
     * Add Resource Logic.
     * @param ext
     *            file extension.
     * @param mime
     *            set MimeType.
     */
    public final void addResourceLogic(final String ext, final String mime) {
        typeMap.put(ext, new ResourceType(mime));
    }

    /**
     * Get last selected file path. Need for usability.
     * @return current selected resource path.
     */
    public final String getSelectorPath() {
        return getPathURI() + "/" + selector;
    }

    /**
     * @param path
     *            URI/URL path from GET action of needle file.
     * @return true if resource it has on server.
     */
    public final boolean isResourceURI(final String path) {
        selector = path;
        final int dot = path.indexOf(".");
        final int lsl = path.lastIndexOf("/");
        if (dot > lsl) {
            final String ext = path.substring(dot + 1, path.length());
            select = getTypeMap().get(ext);
            final File f = new File(getSelectorPath());
            if (select != null && f.isFile()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Get last selected.
     * @return current selected resource.
     */
    public final ResourceType getSelected() {
        return select;
    }

    /**
     * @return the pathURI
     */
    private String getPathURI() {
        return this.pathURI;
    }

    /**
     * @return the typeMap
     */
    private Map<String, ResourceType> getTypeMap() {
        return this.typeMap;
    }
}
